import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import 'element-plus/dist/index.css'
import './assets/css/variables.css'

// 检查用户登录状态
// 从localStorage中恢复用户会话
const token = localStorage.getItem('token');
const userData = localStorage.getItem('userData');
if (token && userData) {
  try {
    const user = JSON.parse(userData);
    store.commit('setUser', user);
  } catch (error) {
    console.error('恢复用户会话失败', error);
    localStorage.removeItem('userData');
    localStorage.removeItem('token');
  }
}

// 导入全局组件
import UserPanel from './components/common/UserPanel.vue'
import ToolCard from './components/ui/ToolCard.vue'

const app = createApp(App)

// 注册所有Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

// 注册全局组件
app.component('UserPanel', UserPanel)
app.component('ToolCard', ToolCard)

app.use(router)
app.use(store)
app.use(ElementPlus, { size: 'default' })

app.mount('#app')
